<?php
//转变时间
function xilumarket_date_change($date)
{
    if (!$date) {
        return '';
    }
    $date = (!is_numeric($date)) ? strtotime($date) : $date;
    if (date('Ymd', $date) == date('Ymd', time())) {
        return date('H:i', $date);
    } else if (date('Ymd', $date) == date('Ymd', strtotime('-1 day'))) {
        return "昨天 " . date('H:i', $date);
    } else if (date('Y', $date) == date('Y', time())) {
        return date('m月d日', $date);
    } else {
        return date('Y年m月d日', $date);
    }
}
//转变时间几  前
function xilumarket_time_tran($show_time)
{
    if (!$show_time) {
        return '';
    }
    $show_time = (!is_numeric($show_time)) ? strtotime($show_time) : $show_time;
    $now_time = time();
    $dur = $now_time - $show_time;
    if ($dur < 0) {
        return $show_time;
    } else {
        if ($dur < 10) {
            return '刚刚';
        } else if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    return xilumarket_date_change($show_time);
                }
            }
        }
    }
}
function xilumarket_time_cha($s_time,$e_time)
{
         $cha = $s_time-$e_time;
         
         //年
         $year = floor($cha  / (31536000));
         if($year>=1){
            return $year."年前";
         }
 
         //月
         $month_total = floor($cha  / 2592000);
         if($month_total>=1){
            return $month_total."月前";
         }
         //日
         $day_total = floor($cha /86400);
        if($day_total>=1){
            return $day_total."天前";
         }
         //时
         $hour_total = floor($cha/3600 );//总相差N小时
        if($hour_total>=1){
            return $hour_total."小时前";
         }
         //分
         $minute_total = floor($cha/60);
        if($minute_total>=1){
            return $minute_total."分钟前";
         }
         //秒
         $second_total = floor($cha);
        if($second_total>=1){
            return $second_total."秒前";
         }
         return '刚刚';
}

/**
 * 获取经纬度
 */
if (!function_exists('xilumarketGetGeo')) {
    /**
     * 根据地址获取经纬度
     */
    function xilumarketGetGeo($address)
    {
        if (empty($address)) {
            return ['status' => 0, 'info' => '地址错误，查询无结果'];
        }
        $lng = $lat = 0;
        $config = \app\common\model\xilumarket\Config::getMyConfig("map");
        if ($config['maptype'] == 'baidu') {
            $return = \fast\Http::get('http://api.map.baidu.com/geocoder/v2/', ['address' => $address, 'output' => 'json', 'ak' => $config['mapkey']]);
            $return = json_decode($return);
            if (!$return->status) {
                $lng = $return->result->location->lng;
                $lat = $return->result->location->lat;
                return ['lng' => $lng, 'lat' => $lat, 'status' => 1];
            }
        } else if ($config['maptype'] == 'tencent') {
            $return = \fast\Http::get('https://apis.map.qq.com/ws/geocoder/v1/', ['address' => $address, 'key' => $config['mapkey']]);
            $return = json_decode($return, true);
            if ($return['status'] == 0) {
                $location = $return['result']['location'];
                return ['lng' => $location['lng'], 'lat' => $location['lat'], 'status' => 1];
            }
        } else {
            $return = \fast\Http::get('https://restapi.amap.com/v3/geocode/geo', ['address' => $address, 'output' => 'json', 'key' => $config['mapkey']]);
            $return = json_decode($return, true);
            if ($return['status']) {
                $location = $return['geocodes'][0]['location'];
                $location_arr = explode(',', $location);
                return ['lng' => $location_arr[0], 'lat' => $location_arr[1], 'status' => 1];
            }
        }
        return ['status' => 0, 'info' => '查询无结果，请检查配置参数'];
    }
}

/*
* 根据经纬度返回地址信息
*/
function xilumarketGetAddr($lat, $lng){
    $config = \app\common\model\xilumarket\Config::getMyConfig('map');
    $address = [];
    if ($config['maptype'] == 'baidu') {
        $return = \fast\Http::get("https://api.map.baidu.com/reverse_geocoding/v3/", [
            'location' => "$lat,$lng",
            'output' => 'json',
            'ak' => $config['mapkey'],
        ]);
        $return = json_decode($return, true);
        if (!$return['status']) {
            $address = [
                'prov' => $return['addressComponent']['province'],
                'city' => $return['addressComponent']['city'],
            ];
        }
    } else if ($config['maptype'] == 'tencent') {
        $return = \fast\Http::get("https://apis.map.qq.com/ws/geocoder/v1/", [
            'location' => "$lat,$lng",
            'key' => $config['mapkey'],
        ]);
        $return = json_decode($return, true);
        if (!$return['status']) {
            $address = [
                'prov' => $return['result']['address_component']['province'],
                'city' => $return['result']['address_component']['city'],
            ];
        }
    } else {
        $return = \fast\Http::get("https://restapi.amap.com/v3/geocode/regeo", [
            'location' => "$lng,$lat",
            'key' => $config['mapkey'],
        ]);
        $return = json_decode($return, true);
        if ($return['status'] == '1') {
            $address = [
                'prov' => $return['regeocode']['addressComponent']['province'],
                'city' => ($return['regeocode']['addressComponent']['city'] && !empty($return['regeocode']['addressComponent']['city'])) ? $return['regeocode']['addressComponent']['city'] : $return['regeocode']['addressComponent']['province'],
            ];
        }
    }
    return $address;
}